<template>
  <div class="consult-pay-page" v-if="orderPreData && patientInfo">
    <cp-nav-bar title="支付" />
    <div class="pay-info">
      <p class="tit">图文问诊 {{ orderPreData?.payment }} 元</p>
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p class="desc">
        <span>极速问诊</span>
        <span>自动分配医生</span>
      </p>
    </div>
    <van-cell-group>
      <van-cell title="优惠券" value="-¥10.00" />
      <van-cell title="积分抵扣" value="-¥10.00" />
      <van-cell
        title="实付款"
        :value="`￥${orderPreData?.payment}`"
        class="pay-price"
      />
    </van-cell-group>
    <div class="pay-space"></div>
    <van-cell-group>
      <van-cell
        title="患者信息"
        :value="`${patientInfo?.name} | ${patientInfo?.genderValue} | ${patientInfo?.age}岁`"
      ></van-cell>
      <van-cell title="病情描述" :label="store.consult.illnessDesc"></van-cell>
    </van-cell-group>
    <div class="pay-schema">
      <van-checkbox v-model="checked"
        >我已同意 <span class="text">支付协议</span></van-checkbox
      >
    </div>
    <van-submit-bar
      :loading="loading"
      @submit="getOrderID"
      button-type="primary"
      :price="orderPreData?.payment ? orderPreData?.payment * 100 : 39"
      button-text="立即支付"
      text-align="left"
    />
  </div>
  <div class="consult-detail-page" v-else>
    <cp-nav-bar title="问诊详情" />
    <van-skeleton title :row="4" style="margin-top: 30px" />
    <van-skeleton title :row="4" style="margin-top: 30px" />
  </div>
  <van-action-sheet
    :closeable="false"
    :before-close="onClose"
    :close-on-popstate="false"
    v-model:show="show"
    title="选择支付方式"
  >
    <div class="pay-type">
      <p class="amount">￥{{ orderPreData?.payment }}</p>
      <van-cell-group>
        <van-cell @click="payMethod = 0" title="微信支付">
          <template #icon><cp-icon name="consult-wechat" /></template>
          <template #extra
            ><van-checkbox :checked="payMethod === 0"
          /></template>
        </van-cell>
        <van-cell @click="payMethod = 1" title="支付宝支付">
          <template #icon><cp-icon name="consult-alipay" /></template>
          <template #extra
            ><van-checkbox :checked="payMethod === 1"
          /></template>
        </van-cell>
      </van-cell-group>
      <div class="btn">
        <van-button type="primary" round block @click="payClick"
          >立即支付</van-button
        >
      </div>
    </div>
  </van-action-sheet>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { useConsultStore } from "@/stores";
import { ConsultType } from "@/enums";
import {
  OrderPreApi,
  getPatientInfo,
  orderIdApi,
  pay,
} from "@/services/consult";
import type { ConsultOrderPreData, patientInfoData } from "@/types/consult";
import { showConfirmDialog, showDialog, showToast } from "vant";
import { useRouter } from "vue-router";
const router = useRouter();
const store = useConsultStore();
const orderPreData = ref<ConsultOrderPreData>();
const patientInfo = ref<patientInfoData>();
const show = ref(false);
onMounted(async () => {
  if (!store.consult.patientId) return;
  const res = await OrderPreApi(ConsultType.Fast, store.consult.illnessType);
  const res1 = await getPatientInfo(store.consult.patientId);
  orderPreData.value = res.data;
  patientInfo.value = res1.data;
});
// 获取订单id
const checked = ref(false);
const orderId = ref("");
const loading = ref(false);
async function getOrderID() {
  if (!checked.value) return showToast("请先同意支付协议");
  loading.value = true;
  const res = await orderIdApi({
    consultFlag: store.consult.consultFlag,
    patientId: store.consult.patientId as string,
    pictures: store.consult.pictures,
    type: store.consult.type,
    illnessDesc: store.consult.illnessDesc as string,
    depId: store.consult.depId as string,
    illnessTime: store.consult.illnessTime,
    illnessType: store.consult.illnessType,
  });
  loading.value = false;
  orderId.value = res.data.id;
  store.clear();
  show.value = true;
}

onMounted(() => {
  if (!store.consult.patientId) {
    return showDialog({
      title: "温馨提示",
      message:
        "问诊信息不完整请重新填写，如有未支付的问诊订单可在问诊记录中继续支付！",
      closeOnPopstate: false,
    }).then(() => {
      // on confirm
      router.push("/");
    });
  }
});

async function onClose() {
  return await showConfirmDialog({
    title: "关闭支付",
    message: "取消支付将无法获得医生回复，医生接诊名额有限，是否确认关闭？",
    cancelButtonText: "仍要关闭",
    confirmButtonText: "继续支付",
  })
    .then(() => {
      return false;
    })
    .catch(() => {
      orderId.value = "";
      router.push("/user/consult");
      return true;
    });
}
// 点击立即支付
const payMethod = ref(0);
async function payClick() {
  const res = await pay(
    payMethod.value,
    orderId.value,
    "http://localhost:5173/room"
  );
  location.href = res.data.payUrl;
}
</script>

<style lang="scss" scoped>
.consult-pay-page {
  padding: 0 0 50px 0;
}
.pay-info {
  display: flex;
  padding: 15px;
  flex-wrap: wrap;
  align-items: center;
  .tit {
    width: 100%;
    font-size: 16px;
    margin-bottom: 10px;
  }
  .img {
    margin-right: 10px;
    width: 38px;
    height: 38px;
    border-radius: 4px;
    overflow: hidden;
  }
  .desc {
    flex: 1;
    > span {
      display: block;
      color: var(--cp-tag);
      &:first-child {
        font-size: 16px;
        color: var(--cp-text2);
      }
    }
  }
}
.pay-price {
  ::v-deep() {
    .vam-cell__title {
      font-size: 16px;
    }
    .van-cell__value {
      font-size: 16px;
      color: var(--cp-price);
    }
  }
}
.pay-space {
  height: 12px;
  background-color: var(--cp-bg);
}
.pay-schema {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  .text {
    color: var(--cp-primary);
  }
}
::v-deep() {
  .van-submit-bar__button {
    font-weight: normal;
    width: 160px;
  }
}
.pay-type {
  .amount {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
  }
  .btn {
    padding: 15px;
  }
  .van-cell {
    align-items: center;
    .cp-icon {
      margin-right: 10px;
      font-size: 18px;
    }
    .van-checkbox :deep(.van-checkbox__icon) {
      font-size: 16px;
    }
  }
}
</style>
